package net.soporte.action;

import java.util.List;
import java.util.Vector;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.soporte.dao.SucursalDAO;
import net.soporte.form.EmpresaForm;
import net.soporte.form.SucursalForm;

import org.apache.struts.action.Action;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SucursalAction extends DispatchAction {
	public ActionForward Agregar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		SucursalForm sucursalForm=(SucursalForm)form;
		//utilizar DAO
		SucursalDAO sucursalDAO = new SucursalDAO();
		if ((sucursalDAO.Insertar(sucursalForm))==0){
			List listSucursales=new Vector();	
			//utilizar DAO			
			listSucursales=sucursalDAO.ListarDatos(form);
			request.setAttribute("ls", listSucursales);
			target = "success";
		}else{
			target = "failure";
		}
		return mapping.findForward(target);
	}
	public ActionForward Actualizar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		SucursalForm sucursalForm = (SucursalForm) form;
		sucursalForm.setDireccion("");
		sucursalForm.setTelefono("");
		sucursalForm.setNombreSucursal("");
		System.out.println("actualizar");
		//utilizar DAO
		SucursalDAO sucursalDAO = new SucursalDAO();
		if ((sucursalDAO.Actualizar(sucursalForm))==0){
			List listSucursales=new Vector();	
			//utilizar DAO			
			listSucursales=sucursalDAO.ListarDatos(form);
			request.setAttribute("ls", listSucursales);
			target = "success";
		}else{
			target = "failure";
		}
		return mapping.findForward(target);
	}

	public ActionForward listarSucursales(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		List listSucursales=new Vector();	
		//utilizar DAO
		SucursalDAO sucursalDAO = new SucursalDAO();
		listSucursales=sucursalDAO.ListarDatos(form);
		target = "success";
		request.setAttribute("ls", listSucursales);
//		if ((sucursalDAO.listarDatos(form)) ==0){
//			target = "success";
//		}else{
//			target = "failure";
//		}
		return mapping.findForward(target);
	}
	public ActionForward Populate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		
		List EmpresasLst=new Vector();

		//dummie
		EmpresasLst.add(new EmpresaForm(1,"Empresa1"));
		EmpresasLst.add(new EmpresaForm(2,"Empresa2"));
		((SucursalForm)form).setEmpresaList(EmpresasLst);
		//SucursalForm sf=new SucursalForm();
		target = "populate";
//		if ((sucursalDAO.listarDatos(form)) ==0){
//			target = "success";
//		}else{
//			target = "failure";
//		}
		return mapping.findForward(target);
	}
	public ActionForward ControlMtto(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		List listSucursales=new Vector();	
		//utilizar DAO
		SucursalDAO sucursalDAO = new SucursalDAO();
		listSucursales=sucursalDAO.ListarDatos(form);
		request.setAttribute("ls", listSucursales);
		target = "success";
		return mapping.findForward(target);
	}
}
